package com.allen.study.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.allen.study.application.api.request.SysEmpRoleQueryRequest;
import com.allen.study.application.api.response.SysEmpRoleQueryResponse;
import com.allen.study.application.repository.ISysEmpRoleReadModelRepo;
import com.allen.study.domain.entity.SysEmpRole;
import com.allen.study.domain.repository.ISysEmpRoleRepo;
import com.allen.study.common.base.Pagination;
import com.allen.study.infrastructure.persistence.assembler.SysEmpRolePOAssembler;
import com.allen.study.infrastructure.persistence.dao.ISysEmpRoleMapper;
import com.allen.study.infrastructure.persistence.po.SysEmpRolePO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户角色关联表
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class SysEmpRoleRepoImpl extends ServiceImpl<ISysEmpRoleMapper, SysEmpRolePO>
        implements ISysEmpRoleRepo, ISysEmpRoleReadModelRepo {

    /**
     * 用户角色关联表持久化对象结构映射
     */
    private final SysEmpRolePOAssembler sysEmpRolePOAssembler;

    @Override
    public List<SysEmpRoleQueryResponse> query(Pagination pagination, SysEmpRoleQueryRequest queryRequest) {
        // 构造查询条件
        LambdaQueryWrapper<SysEmpRolePO> lambdaQueryWrapper = Wrappers.lambdaQuery();


        // 如果 员工id 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEmployeeId())) {
            lambdaQueryWrapper.eq(SysEmpRolePO::getEmployeeId, queryRequest.getEmployeeId());
        }
        // 如果 角色key 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getRoleKey())) {
            lambdaQueryWrapper.eq(SysEmpRolePO::getRoleKey, queryRequest.getRoleKey());
        }

        // 默认按照主键倒序排序
        lambdaQueryWrapper.orderByDesc(SysEmpRolePO::getId);

        // 分页查询
        Page<SysEmpRolePO> page = super.page(Page.of(pagination.getPageNumber(), pagination.getPageSize()), lambdaQueryWrapper);
        pagination.setTotal(page.getTotal());
        // 用户角色关联表持久化对象 转 用户角色关联表查询响应对象
        return sysEmpRolePOAssembler.toQueryResponse(page.getRecords());
    }

    @Override
    public SysEmpRole queryById(String sysEmpRoleId) {
        // 根据主键查询用户角色关联表
        SysEmpRolePO sysEmpRolePO = super.getById(sysEmpRoleId);
        // 用户角色关联表持久化对象 转 用户角色关联表响应对象
        return sysEmpRolePOAssembler.toEntity(sysEmpRolePO);
    }

    @Override
    public List<SysEmpRole> queryAll() {
        // 根据主键查询用户角色关联表
        List<SysEmpRolePO> list = super.list();
        // 用户角色关联表持久化对象 转 用户角色关联表响应对象
        return sysEmpRolePOAssembler.toEntity(list);
    }

    @Override
    public List<SysEmpRole> queryByRoleKey(String roleKey) {
        LambdaQueryWrapper<SysEmpRolePO> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(SysEmpRolePO::getRoleKey, roleKey);
        return sysEmpRolePOAssembler.toEntity(super.list(lambdaQueryWrapper));
    }

    @Override
    public void create(SysEmpRole sysEmpRole) {
        // 用户角色关联表实体转用户角色关联表持久化对象
        SysEmpRolePO sysEmpRolePO = sysEmpRolePOAssembler.assembler(sysEmpRole);

        // 创建用户角色关联表
        super.save(sysEmpRolePO);
    }

    @Override
    public void deleteById(String sysEmpRoleId) {
        // 物理删除用户角色关联表
        super.removeById(sysEmpRoleId);
    }

    @Override
    public void updateById(SysEmpRole sysEmpRole) {
        // 用户角色关联表实体转用户角色关联表持久化对象
        SysEmpRolePO sysEmpRolePO = sysEmpRolePOAssembler.assembler(sysEmpRole);
        // 更新用户角色关联表
        super.updateById(sysEmpRolePO);
    }
}
